<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { luckDrawApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  import addForm from './addForm.vue';
  import JackpotList from './JackpotList.vue';
  import Pagination from "@/components/Pagination/index.vue";
  const addFormRef = ref(),
  JackpotListRef = ref()
  const action = reactive({
    total:0,
    list:[] as any[],
  })
  const handleForm = (row={})=>{
    addFormRef.value.openDialog(row)
  }
  const handleDel = (row:any={})=>{
    const { id } = row
    if(!id) return ToastWarning('缺少id')
    ElMessageBox.confirm(`确认删除id：${id}礼物池吗?`, '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(async() => {
      const loading = Toastloading()
      await luckDrawApi.delActivityPooldefender({
        id
      })
      loading.close()
    }).catch(() => { })
  }
  /**
   * 更新礼物池
   */
  const updateJackpot = (row:any={})=>{
    const { desc='' } = row
    ElMessageBox.confirm(`确定更新礼物池：${desc}吗？`, '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
      const loading = Toastloading()
      luckDrawApi.setActivityPooldefender({
        pools:JSON.stringify([row])
      }).then(()=>{
        ToastSuccess(`更新礼物池：${desc}成功`)
        loading.close()
      }).catch(()=>{
        loading.close()
      })
    }).catch(() => { })
  }
  /**
   * 礼物池提交
   */
  const jackpotSumit = (query:any = {})=>{
    let { nowIndex, tableDataTwo } = query
    action.list[nowIndex].pool = tableDataTwo;
  }
  const checkPage = (scoped:any={})=>{
    const { row={}, $index:nowIndex=-1 } = scoped
    if(nowIndex < 0) return
    JackpotListRef.value.openDialog({
      ...row,
      nowIndex
    })
  }
  const listQuery = reactive({
    page:1,
    limit:20,
  })
  const getList = async()=> {
    const loading = Toastloading()
    const res = await luckDrawApi.getActivityPooldefender()
    loading.close()
    const { data=[], total=0, } = res?.data || {}
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <!-- <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <el-button type="primary" @click="handleForm()">新增礼物池</el-button>
        </div>
      </div>
    </div> -->
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="desc" label="礼物池描述" width="200" />
      <el-table-column prop="luck_id" label="玩法活动ID" width="100" />
      <el-table-column prop="weight" label="礼物池" width="100">
        <template #default="scoped">
          <el-button type="primary" @click="checkPage(scoped)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="180">
        <template #default="scoped">
          <el-tag>{{ scoped.row.status === 1 ? "开启中" : "关闭" }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column min-width="180" prop="updated_at" label="操作时间" />
      <el-table-column min-width="180" prop="created_at" label="创建时间" />
      <el-table-column label="操作" fixed="right" width="180">
          <template #default="scoped">
            <!-- <el-button type="primary" @click="handleForm(scoped.row)">编辑</el-button> -->
            <!-- <el-button type="danger" @click="handleDel(scoped.row)">删除</el-button> -->
            <el-button class="xm-mt10" type="primary" @click="updateJackpot(scoped.row)">更新礼物池</el-button>
          </template>
        </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />

    <addForm ref="addFormRef" @update-list="getList" />
    <JackpotList ref="JackpotListRef" @submit="jackpotSumit" />
  </div>
</template>
